package com.example.word.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dict.R;
import com.example.word.bean.Browse;
import com.example.word.bean.Word;
import com.example.word.util.CommonUtils;
import com.example.word.util.DateUtils;
import com.example.word.util.HttpUtils;
import com.example.word.util.SPUtils;
import com.example.word.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 词库单元适配器
 */

public class WordReviewAdapter extends RecyclerView.Adapter<WordReviewAdapter.ViewHolder> {
    private List<Word> list = new ArrayList<>();
    private Word currentWord = new Word();
    private Context mActivity;
    private ItemListener mItemListener;
    private RecyclerView rvNewsList;
    private String account;

    public void setItemListener(ItemListener itemListener) {
        this.mItemListener = itemListener;
    }

    public WordReviewAdapter(RecyclerView rvNewsList) {
        this.rvNewsList = rvNewsList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mActivity = viewGroup.getContext();
        account = (String) SPUtils.get(mActivity, SPUtils.ACCOUNT, "");
        View view = LayoutInflater.from(mActivity).inflate(R.layout.item_rv_word_review_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        // Word word = list.get(i);

        viewHolder.titleAll.setText(currentWord.getWord());
        viewHolder.dictNameAll.setText(currentWord.getDict());
        viewHolder.ukphoneAll.setText("\uD83D\uDD08英 [" + currentWord.getUkphone() + "]");
        viewHolder.usphoneAll.setText("\uD83D\uDD08美 [" + currentWord.getUsphone() + "]");
        // 此处设置单词释义和例句
        if (StringUtils.isNotEmpty(currentWord.getSentences())) {
            viewHolder.tv_sentencesAll.setText(currentWord.getSentences()
                    .replace("<enzh>", "\r\n")
                    .replace("<split>", "\r\n\r\n"));
        }
        if (StringUtils.isNotEmpty(currentWord.getWordZh())) {
            viewHolder.tv_wordZhAll.setText(currentWord.getWordZh().replace("<split>", "\r\n\r\n"));
        }
        viewHolder.ukphoneAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.ukPhoneClick(currentWord);
                }
            }
        });
        viewHolder.usphoneAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.usPhoneClick(currentWord);
                }
            }
        });
        viewHolder.titleAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.titleClick(currentWord);
                }
            }
        });

        viewHolder.title.setText(currentWord.getWord());
        viewHolder.ukphone.setText("\uD83D\uDD08英 [" + currentWord.getUkphone() + "]");
        viewHolder.usphone.setText("\uD83D\uDD08美 [" + currentWord.getUsphone() + "]");
        viewHolder.ukphone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.ukPhoneClick(currentWord);
                }
            }
        });
        viewHolder.usphone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.usPhoneClick(currentWord);
                }
            }
        });
        viewHolder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.titleClick(currentWord);
                }
            }
        });

        viewHolder.reviewWord.setVisibility(View.VISIBLE);
        viewHolder.reviewAll.setVisibility(View.GONE);
        viewHolder.reviewWord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.reviewWord.setVisibility(View.GONE);
                viewHolder.reviewAll.setVisibility(View.VISIBLE);
            }
        });
    }

    public void addItem(List<Word> listAdd) {
        //如果是加载第一页，需要先清空数据列表
        this.list.clear();
        if (listAdd != null) {
            //添加数据
            this.list.addAll(listAdd);
        }
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }

    public void setCurrentWord(Word word, Activity activity) {
        // 来到这里就说明这个单词已经显示在页面上了。保存为已复习
        if (word == null) return;

        Map<String, String> requestParam = new HashMap<>();
        requestParam.put("word", word.getWord());
        // 调用 API 客户端的方法
        HttpUtils.sendGetRequest("/saveBrowse", requestParam, activity, Browse.class, response -> { });

        this.currentWord = word;
        this.list.clear();
        this.list.add(this.currentWord);
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title, ukphone, usphone;

        private TextView titleAll, dictNameAll, ukphoneAll, usphoneAll, tv_sentencesAll, tv_wordZhAll;

        private LinearLayout reviewWord, reviewAll;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            titleAll = itemView.findViewById(R.id.titleAll);
            dictNameAll = itemView.findViewById(R.id.dictNameAll);
            ukphoneAll = itemView.findViewById(R.id.ukphoneAll);
            usphoneAll = itemView.findViewById(R.id.usphoneAll);
            tv_sentencesAll = itemView.findViewById(R.id.tv_sentencesAll);
            tv_wordZhAll = itemView.findViewById(R.id.tv_wordZhAll);
            title = itemView.findViewById(R.id.title);
            ukphone = itemView.findViewById(R.id.ukphone);
            usphone = itemView.findViewById(R.id.usphone);

            reviewWord = itemView.findViewById(R.id.reviewWord);
            reviewAll = itemView.findViewById(R.id.reviewAll);

        }
    }

    public interface ItemListener {
        void ukPhoneClick(Word word);

        void usPhoneClick(Word word);

        void titleClick(Word word);
    }
}
